package com.seanLab.tool;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.seanLab.domain.TagInfo;
import com.seanLab.dto.RecommendArticleDto;
import com.seanLab.dto.SuggestArticleKeywordsDto;
import com.seanLab.dto.SuggestModelImageDto;
import com.seanLab.dto.SuggestModelArticleDto;

import java.io.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class TestTagSuggestor {
    private static final Type POST_TYPE = new TypeToken<List<SuggestModelArticleDto>>() {}.getType();
    private static final String DATASET_PATH = "dataset";
    private static final String DATASET = "sina.article_xiaobo2.json";
    private static final String TestArticle = "workingDir" + File.separator + "TestArticle.txt";
    private static final String TestOutput = "workingDir" + File.separator + "TestOutput.txt";

    List<SuggestModelArticleDto> dataset;
    TagSuggestor tagSuggestor = new TagSuggestor();

    private void loadDataset() throws IOException{
        dataset = new Gson().fromJson(
                new JsonReader(new FileReader(DATASET_PATH + File.separator + DATASET)), POST_TYPE);
    }

    private void testTrain() throws IOException{
        System.out.println(tagSuggestor.train(dataset));
    }

    private void testLoad() {
        System.out.println(tagSuggestor.loadModel("workingDir" + File.separator + "model_22029"));
    }

    private void testTag(int articleID) {
        SuggestModelArticleDto article = dataset.get(articleID);
        System.out.println(article.getContent());
        ArrayList<SuggestModelImageDto> images = new ArrayList<>();
        images.add(new SuggestModelImageDto());
        images.add(new SuggestModelImageDto());
        article.setSuggestModelImageDtoList(images);
        List<List<TagInfo>> tagsList = tagSuggestor.doTag(article);
        for (List<TagInfo> tags : tagsList) {
            System.out.println("!!!! SuggestModelImageDto " + tagsList.indexOf(tags) + " !!!!");
            int cnt = 0;
            for (TagInfo tag : tags) {
                System.out.println(tag.getTagName() + " " + tag.getTagScore());
                if (++cnt > 10) break;
            }
            System.out.println();
        }

    }

    private void testTag() {
        testTag(0);
    }

    private void testSuggest(int articleID) {
        RecommendArticleDto article = new RecommendArticleDto();
        article.setTitle(dataset.get(articleID).getTitle());
        article.setContent(dataset.get(articleID).getContent());
        System.out.println(article.getContent());
        long startTime = System.currentTimeMillis();
        List<SuggestArticleKeywordsDto> tags = tagSuggestor.doSuggest(article);
        long endTime = System.currentTimeMillis();
        System.out.println("Time: " + String.valueOf(endTime - startTime));
        int cnt = 0;
        for (SuggestArticleKeywordsDto tag : tags) {
            System.out.println(tag.getKeywords() + " " + tag.getScore());
            if (++cnt > 10) break;
        }
    }

    private void testSuggest() throws IOException {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(
                                TestArticle),
                        "UTF-8"));

        String title = reader.readLine();
        String content = reader.readLine();

        RecommendArticleDto p = new RecommendArticleDto();
        p.setTitle(title);
        p.setContent(content);

        long startTime = System.currentTimeMillis();
        List<SuggestArticleKeywordsDto> tags = tagSuggestor.doSuggest(p);
        long endTime = System.currentTimeMillis();
        System.out.println("Time: " + String.valueOf(endTime - startTime));

        BufferedWriter outTag = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(TestOutput),
                "UTF-8"));
        int cnt =  0;
        for (SuggestArticleKeywordsDto ws : tags) {
            outTag.write(ws.getKeywords() + " " + ws.getScore());
            System.out.println(ws.getKeywords() + " " + ws.getScore());
            ++ cnt;
            if (cnt>10) break;
            outTag.newLine();
            outTag.flush();
        }
    }

    public static void main(String[] args) throws IOException{

        TestTagSuggestor test = new TestTagSuggestor();
//        test.loadDataset();
//        test.testTrain();
        test.testLoad();
        //test.testTag();
        test.testSuggest();
//        test.testSuggest(0);
    }
}
